package com.shop2cn.iapi.wxcomponent.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.wxcomponent.parameter.ApplyChargeBean;
import com.shop2cn.iapi.wxcomponent.service.ApplyChargeCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*独立小程序商家付费开通Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ApplyCharge{
	private static ApplyChargeBean applychargeBean;
	private static ApplyChargeCall applychargeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("独立小程序商家付费开通Test");
	}

	@Before
	public void caseUp() {
		applychargeBean=new ApplyChargeBean();
		applychargeCall=new ApplyChargeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applychargeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_001() {
		Logger.start(true,"申请付费，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            applychargeBean.setUserId(mchid);
			applychargeBean.setMchId(mchid);
			applychargeBean.setChargeIp(TestDataManager.getRandomIp());
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();	
			
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			Logger.debug(FormatUtil.beanToGSONString(new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid)));
			Logger.verifyNotEquals("[]", new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).toString(),"申请付费后，数据库产生付费订单");
			Logger.verifyEquals(WxComponentService.defmoney, new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).get(0).getMoney().doubleValue(),"申请付费后，查询需要付费的钱数");
			Logger.verifyEquals(0, new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).get(0).getChargeStatus(),"申请付费后，查询订单状态");
		} catch (Exception e) {
			
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_002() {
		Logger.start(true,"申请付费，缺少ip地址，失败");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            WxComponentService.ConfirmAuditMchApp(mchid);
            WxComponentService.Remark("mark", mchid);
            applychargeBean.setUserId(mchid);
			applychargeBean.setMchId(mchid);
//			applychargeBean.setChargeIp(TestDataManager.getRandomIp());
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();	
			
			Logger.verifyEquals("400", applychargeCall.getString("code"), "验证code");
			Logger.verifyEquals("[]", new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).toString(),"申请付费后，数据库产生付费订单");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_003() {
		Logger.start(true,"缺少返回url，失败");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            WxComponentService.ConfirmAuditMchApp(mchid);
            WxComponentService.Remark("mark", mchid);
			
            applychargeBean.setUserId(mchid);
			applychargeBean.setMchId(mchid);
			applychargeBean.setChargeIp(TestDataManager.getRandomIp());
//			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			 
			Logger.verifyEquals("400", applychargeCall.getString("code"), "验证code");
			Logger.verifyEquals("[]", new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).toString(),"申请付费后，数据库产生付费订单");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_004() {
		Logger.start(true,"回调url不是正确格式，不做校验，成功");
		try {
			
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
            WxComponentService.ConfirmAuditMchApp(mchid);
            WxComponentService.Remark("mark", mchid);
            applychargeBean.setUserId(mchid);
			applychargeBean.setMchId(mchid);
			applychargeBean.setChargeIp(TestDataManager.getRandomIp());
			applychargeBean.setCallbackUrl("haap://www.ymatou.com");
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			Logger.verifyNotEquals("[]", new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).toString(),"申请付费后，数据库产生付费订单");
			Logger.verifyEquals(WxComponentService.defmoney, new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).get(0).getMoney().doubleValue(),"申请付费后，查询需要付费的钱数");
			Logger.verifyEquals(0, new DefGodMchWapper().selectMchChargeRecordIdByExample(mchid).get(0).getChargeStatus(),"申请付费后，查询订单状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}